package com.lc.utils;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
 * Created by lichao on 2017/6/1.
 */
    /**
     *
     * <p class="detail">
     * 功能：代理类：统一处理时间——约定大于配置
     * </p>
     * @ClassName: KingProxy
     * @version V1.0
     * @date 2014年12月17日
     * @author <a href="mailto:wangs@financegt.com">kings</a>
     */
    public class KingProxy implements InvocationHandler, Serializable {

        private static final long serialVersionUID = 1993713162421775843L;

        private Object target;

        public void setTarget(Object target) {
            this.target = target;
        }

        @Override
        public Object invoke(Object o, Method method, Object[] args)
                throws Throwable {
            if (!Object.class.equals(method.getDeclaringClass())) {
                try {
                    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
                    String name = Util.pasName(method.getDeclaringClass().getName())+"."+request.getHeader("live-license");
                    target = MyApplicationContext.getBean(name);
                    if(target==null){
                        name = Util.pasName(method.getDeclaringClass().getName())+".default";
                        target = MyApplicationContext.getBean(name);
                    }
                    if(target==null){
                        System.out.println("-------------------------没有此类");
                    }
                    Object result = method.invoke(target, args);
                    System.out.println("---------------------before");
                    return result;

                } catch (Exception e) {
                    throw e;
                }
            }
            System.out.println("e___________________proxy___________________");
            /**
             * PersonalDeal
             * 统一处理事情
             * */
            return null;
        }
        /**
         * 获取目标对象的代理对象
         * @return 代理对象
         */
        public Object getProxy() {
            return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                    target.getClass().getInterfaces(), this);
        }

        /*@Override
        public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
            if (!Object.class.equals(method.getDeclaringClass())) {
                try {
                    *//*String name = proxy.getClass().getName()+".bl";
                    Object clazz = MyApplicationContext.getBean(name);
                    clazz.getClass().getMethod("",)*//*
                    System.out.println("______________________proxy___________________");
                    return methodProxy.invokeSuper(o, objects);
                } catch (Exception e) {
                    throw e;
                }
            }
            System.out.println("e___________________proxy___________________");
            *//**
             * PersonalDeal
             * 统一处理事情
             * *//*
            return null;
        }*/
    }
